package com.starhub.integration.sdk.custom.core;

import okhttp3.OkHttpClient;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * 私有化重排序模型连接器
 * @version: V1.0
 * @author: liuhf
 * @time 2025-08-08
 */
@Component
public class CustomRerankerClientConnector {
    private static OkHttpClient okHttpClient;
    private static final Map<String, CustomRerankerClient> clientMap = new ConcurrentHashMap<>();

    private synchronized OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .build();
        }
        return okHttpClient;
    }

    /**
     * 获取私有化重排序模型客户端
     * @param mark 模型标识
     * @param apiHost API主机地址
     * @param modelName 模型名称
     * @param apiPath API路径
     * @return 重排序客户端
     */
    public synchronized CustomRerankerClient getCustomRerankerClient(String mark, String apiHost, String modelName, String apiPath) {
        String clientKey = mark + "_" + apiHost + "_" + modelName;
        CustomRerankerClient client = clientMap.get(clientKey);

        if (client == null) {
            client = CustomRerankerClient.builder()
                    .okHttpClient(getOkHttpClient())
                    .apiHost(apiHost)
                    .modelName(modelName)
                    .apiPath(apiPath)
                    .build();

            clientMap.put(clientKey, client);
        }

        return client;
    }
} 